<?php

use Biz\model\brandActivity\BlindBoxLottery as mBlindBoxLottery;
use Biz\service\brand\BlindBox;
use Doraemon\model\Member;
use Doraemon\service\User;

/**
 * 中奖动态
 */
class LotteryDynamicAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $prizeList = BlindBox::getPrizeList(0);
        unset($prizeList[BlindBox::EMPTY_BOX_PRIZE_ID]);
        $prizeIds = array_keys($prizeList);
        $prizeIds = array_unique($prizeIds);
        $where = [
            'prize_id' => $prizeIds,
            'lottery_state[!]' => mBlindBoxLottery::STATUS_DELETE,
            'addtime[>=]' => date('Y-m-d', strtotime('-2 days')),
            'addtime[<=]' => date('Y-m-d H:i:s'),
            'LIMIT' => [0, 30],
            'ORDER' => ['prize_price' => 'DESC', 'addtime' => 'DESC'],
        ];
        $lotteryList = mBlindBoxLottery::selectByWhere($where);
        $list = [];
        if ($lotteryList) {
            $uids = array_column($lotteryList, 'uid');
            $userList = User::getBatchBasicUserInfo($uids);
            $userList = array_column($userList, null, 'uid');
            foreach ($lotteryList as $row) {
                $userInfo = isset($userList[$row['uid']]) ? $userList[$row['uid']] : [];
                if ($userInfo) {
                    $list[] = [
                        'uid' => $row['uid'],
                        'avatar' => $userInfo['avatar'],
                        'desc' => $userInfo['nick'] . ' 开出了' . $row['prize_title']
                    ];
                }
            }
            if (count($list) < 30) {
                $num = 30 - count($list);
                self::getRandUserCoverImg($list, $num, $prizeList);
            }
        }
        $keys = array_keys($list);
        //打乱数组的键排序
        shuffle($keys);
        $result = [];
        foreach ($keys as $key) {
            $result[$key] = $list[$key];
        }
        $this->setSuccess(['list' => array_values($result)]);
    }

    private function getRandUserCoverImg(&$list, $num = 1, $prizeList)
    {
        $prizeCount = count($prizeList);
        $num = $num > $prizeCount ? $prizeCount : $num;
        $prizeIds = array_rand($prizeList, $num);
        $prizeTitles = [];
        foreach ($prizeIds as $prizeId) {
            if (isset($prizeList[$prizeId])) {
                $prizeTitles[] = $prizeList[$prizeId]['prize_title'];
            }
        }
        //获取 机器人uid集合
        $uids = \Biz\tools\helper\Common::getMechanicUids();
        $randIdex = array_rand($uids, $num + 5);
        $randUids = [];
        foreach ($randIdex as $index) {
            $randUids[] = $uids[$index];
        }
        $memberList = Member::getUserInfoByIds($randUids);
        $imgs = [];
        $i = 0;
        foreach ($memberList as $row) {
            $avatarImgs = User::getUserAvatarPicUrl($row['uid'], $row['avatar_version']);
            if ($avatarImgs && isset($prizeTitles[$i])) {
                $list[] = [
                    'uid' => $row['uid'],
                    'avatar' => $avatarImgs[0],
                    'desc' => $row['nick'] . ' 开出了' . $prizeTitles[$i],
                    'r' => 1,
                ];
                $i++;
            }
        }
        return array_slice($imgs, 0, $num);
    }
}